package com.yu.subject.application.controller;

import com.yu.subject.application.entity.SubjectInfoDTO;
import com.yu.subject.common.entity.PageResult;
import com.yu.subject.common.entity.Result;
import com.yu.subject.common.exception.CodeAndMessage;
import com.yu.subject.common.exception.JavaSmException;
import com.yu.subject.common.util.BeanCopyUtil;
import com.yu.subject.domain.entity.SubjectAnswerBO;
import com.yu.subject.domain.entity.SubjectInfoBO;
import com.yu.subject.domain.service.SubjectInfoDomainService;
import com.yu.subject.infra.basic.entity.SubjectMapping;
import com.yu.subject.infra.basic.service.SubjectMappingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 路亚
 * @version 0.1
 * @className SubjectInfoController
 * @description
 * @date 2024/12/24 21:35
 * @since jdk11
 */
@RestController
@RequestMapping("/subject")
@Slf4j
@RequiredArgsConstructor
public class SubjectController {

    private final SubjectInfoDomainService subjectInfoDomainService;

    private final SubjectMappingService subjectMappingService;

    /**
     * 新增题目
     */
    @PostMapping("/add")
    public Result add(@RequestBody SubjectInfoDTO subjectInfoDTO){
        if (log.isInfoEnabled()){
            log.info("新增题目，参数：{}", subjectInfoDTO);
        }
        if (StringUtils.isEmpty(subjectInfoDTO.getSubjectName())){
            throw new JavaSmException(CodeAndMessage.SUBJECT_NAME_NOT_NULL);
        }
        if (subjectInfoDTO.getSubjectDifficult() == null){
            throw new JavaSmException(CodeAndMessage.SUBJECT_DIFFICULT_NOT_NULL);
        }
        if (subjectInfoDTO.getSubjectType() == null){
            throw new JavaSmException(CodeAndMessage.SUBJECT_TYPE_NOT_NULL);
        }
        if (subjectInfoDTO.getSubjectScore() == null){
            throw new JavaSmException(CodeAndMessage.SUBJECT_SCORE_NOT_NULL);
        }
        if (CollectionUtils.isEmpty(subjectInfoDTO.getCategoryIds())){
            throw new JavaSmException(CodeAndMessage.CATEGORY_ID_NOT_NULL);
        }
        if (CollectionUtils.isEmpty(subjectInfoDTO.getLabelIds())){
            throw new JavaSmException(CodeAndMessage.LABEL_ID_NOT_NULL);
        }
        SubjectInfoBO subjectInfoBO = new SubjectInfoBO();
        BeanUtils.copyProperties(subjectInfoDTO, subjectInfoBO);
        subjectInfoBO.setOptionList(BeanCopyUtil.copyListProperties(subjectInfoDTO.getOptionList(), SubjectAnswerBO.class));
        subjectInfoDomainService.add(subjectInfoBO);
        return Result.ok();
    }

    /**
     * 查询题目列表
     */
    @GetMapping("/getSubjectPage")
    public Result getSubjectPage(@RequestBody SubjectInfoDTO subjectInfoDTO){
        if (log.isInfoEnabled()){
            log.info("查询题目列表，参数：{}", subjectInfoDTO);
        }
        if (subjectInfoDTO.getCategoryId() == null){
            throw new JavaSmException(CodeAndMessage.CATEGORY_ID_NOT_NULL);
        }
        if (subjectInfoDTO.getLabelId() == null){
            throw new JavaSmException(CodeAndMessage.LABEL_ID_NOT_NULL);
        }
        SubjectInfoBO subjectInfoBO = new SubjectInfoBO();
        BeanUtils.copyProperties(subjectInfoDTO, subjectInfoBO);
        PageResult<SubjectInfoBO> pageResult = subjectInfoDomainService.getSubjectPage(subjectInfoBO);
        return Result.ok(pageResult);
    }

    /**
     * 查询题目详细信息
     */
    @GetMapping("/querySubjectInfo")
    public Result querySubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO){
        if (log.isInfoEnabled()){
            log.info("查询题目详细信息，参数：{}", subjectInfoDTO);
        }
        if (subjectInfoDTO.getId() == null){
            throw new JavaSmException(CodeAndMessage.SUBJECT_ID_NOT_NULL);
        }
        SubjectInfoBO subjectInfoBO = new SubjectInfoBO();
        BeanUtils.copyProperties(subjectInfoDTO, subjectInfoBO);
        SubjectInfoBO boResult = subjectInfoDomainService.querySubjectInfo(subjectInfoBO);
        SubjectInfoDTO result = new SubjectInfoDTO();
        BeanUtils.copyProperties(boResult, result);
        return Result.ok(result);
    }
}
